<template>

  <Modal
      width="800"
      v-model="visible"
      :title="title"
      :key="key"
  >
    <Spin size="large" fix v-if="Loading"></Spin>
    <Form ref="model" :model="model" :rules="rules" inline :label-width="210" :disabled=disableSubmit>
      <Row>
        <FormItem label="收入所属:" prop="ssba0001">
          <c-dict-select v-model="model.ssba0001" dicType="service" :width="400"
                         :dictService="url.getAssistanceMemberList"
                         :serviceParam="serviceParam"/>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="收入类型:" prop="ssba0010">
          <Select class="itemStyle" placeholder="请选择收入类型" @on-change="classifySelect" :disabled="!model.ssba0001"
                  v-model="model.ssba0010" clearable>
            <Option v-for="item in classifyList" :value="item.id" :key="item.id">{{ item.iccf0001 }}
            </Option>
          </Select>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="收入详细分类:" prop="ssba0011">
          <Select class="itemStyle" placeholder="请选择收入详细分类" @on-change="typeSelect"
                  v-model="model.ssba0011" clearable>
            <Option v-for="item in typeList" :value="item.id" :key="item.id">{{ item.iccf0001 }}
            </Option>
          </Select>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="收入计算方式:" prop="ssba0012">
          <Select class="itemStyle" placeholder="请选择收入计算方式" @on-change="waySelect"
                  v-model="model.ssba0012" clearable>
            <Option v-for="item in wayList" :value="item.id" :key="item.id">{{ item.iccf0001 }}
            </Option>
          </Select>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="务工月数:" prop="ssba0016" v-if="model.ssba0011=='0106'">
          <Select class="itemStyle" placeholder="请选择务工月数" @on-change="changeSsba0016"
                  v-model="model.ssba0016" clearable>
            <Option v-for="item in 12" :value="item" :key="item">{{ item }}
            </Option>
          </Select>
        </FormItem>
      </Row>

      <Row v-if="model.ssba0011=='0304'">
        <FormItem label="被征地农民养老保险待遇:" prop="ssba0014">
          <InputNumber :min="0" v-model="model.ssba0014" class="itemStyle" placeholder="请输入被征地农民养老保险待遇"
                       @on-change="change0014"/>
        </FormItem>
      </Row>
      <Row v-if="model.ssba0011=='0305'">
        <FormItem label="职工供养直系亲属生活救济费:" prop="ssba0015">
          <InputNumber :min="0" v-model="model.ssba0015" class="itemStyle"
                       placeholder="请输入职工供养直系亲属生活救济费"
                       @on-change="change0015"/>
        </FormItem>
      </Row>
      <Row>
        <FormItem label="收入计算说明:" prop="ssba0013">
          <c-text-area v-model="model.ssba0013" :max-length="100" :row=3 placeholder="请收入计算说明"
                       class="itemStyle"/>
        </FormItem>
      </Row>

      <Row>
        <FormItem label="月收入金额:" prop="ssba0006">
          <InputNumber :min="0"
                       v-model="model.ssba0006" placeholder="请输入月收入金额" class="itemStyle"
                       :disabled="ssba0006Disable"></InputNumber>
        </FormItem>
      </Row>
    </Form>
    <div slot="footer">
      <Button @click="cancel">取消</Button>
      <template v-if="!disableSubmit">
        <Button type="primary" @click="ok">确定</Button>
      </template>
    </div>
  </Modal>

</template>

<script>
import {CommonMixin} from '@/views/common/js/CommonMixin'
import {postAction} from '@/api/request'

export default {
  mixins: [CommonMixin],
  components: {},
  name: "ss11303-modal",
  data() {
    const validatessba0006 = (rule, value, callback) => {
      if (!this.model.ssba0006 && rule.required && this.model.ssba0006 != 0) {
        callback(new Error('请输入月收入金额'));
      } else {
        callback();
      }
    };
    const validatessba0014 = (rule, value, callback) => {
      if (!this.model.ssba0014 && rule.required) {
        callback(new Error('请输入被征地农民养老保险待遇'));
      } else {
        callback();
      }
    };
    const validatessba0015 = (rule, value, callback) => {
      if (!this.model.ssba0015 && rule.required) {
        callback(new Error('请输入职工供养直系亲属生活救济费'));
      } else {
        callback();
      }
    };
    return {
      serviceParam: {
        azcp0500: this.$parent.queryParam.azcp0500,
      },
      ssba0006Disable: true,
      visible: false,
      disableSubmit: false,
      title: "",
      model: {
        ssba0012: '',
      },
      show0015: false,
      show0014: false,
      key: "",
      Loading: false,
      incomeList: [],
      classifyList: [],
      typeList: [],
      wayList: [],
      url: {
        update: '',
        insert: '',
        load0006: '/api/ac/subsistence/ss11003Service/load0006',
        getAssistanceMemberList: ''
      },
      rules: {
        ssba0001: [
          {required: true, message: '请选择收入所属人', trigger: 'change'}
        ],
        ssba0006: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入月收入金额',
            type: 'number',
            validator: validatessba0006
          },
        ],
        ssba0010: [
          {required: true, message: '请选择收入类型', trigger: 'change'}
        ],
        ssba0011: [
          {required: true, message: '请选择收入详细分类', trigger: 'change'}
        ],
        ssba0012: [
          {required: true, message: '请选择收入计算方式', trigger: 'change'}
        ],
        ssba0013: [
          {required: true, message: '请输入收入计算说明', trigger: 'blur'}
        ],
        ssba0016: [
          {required: true, message: '请选择务工月数', trigger: 'change', type: 'number'}
        ],
        ssba0014: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入被征地农民养老保险待遇',
            type: 'number',
            validator: validatessba0014
          },
        ],
        ssba0015: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入职工供养直系亲属生活救济费',
            type: 'number',
            validator: validatessba0015
          },
        ],
      },
    }
  },
  props: {
    type: {
      type: Number,
      default: 11
    },
  },

  methods: {
    cancel() {
      this.visible = false;
    },
    ok() {
      var that = this;
      this.$refs['model'].validate((valid) => {
        if (valid) {
          that.submitFrom();
        }
      })
    },
    submitFrom() {
      var that = this;
      that.Loading = true;
      var url = this.url.insert;
      if (that.model.id) {
        url = this.url.update;
      }
      that.model.azdf0007 = this.$parent.queryParam.azdf0007;
      // if(that.model.ssba0016){
      //   that.model.ssba0016=parseInt(that.model.ssba0016);
      // }
      console.log( that.model);
      postAction(url, that.model).then((res) => {
        if (res.data.errcode != 0) {
          this.$Message.error(res.data.errmsg);
        } else {
          that.$Message.success("保存完成！");
          this.$emit('ok');
          this.visible = false;
        }
      }).finally(() => {
        that.Loading = false;
      });
    },
    add() {
      this.edit({});
    },
    edit(row) {
      this.url.update = "/api/ac/subsistence/ss" + this.type + "003Service/updateData";
      this.url.insert = "/api/ac/subsistence/ss" + this.type + "003Service/insertData";
      this.url.getAssistanceMemberList = "/api/ac/subsistence/ss" + this.type + "002Service/getAssistanceMemberList";
      row.azcp0500 = this.$parent.queryParam.azcp0500;
      this.model = Object.assign({}, row);
      this.setIncomeList();
      if (!this.model.ssba0006) {
        this.model.ssba0006 = null;
      }
      if (!this.model.ssba0014) {
        this.model.ssba0014 = null;
      }
      if (!this.model.ssba0015) {
        this.model.ssba0015 = null;
      }
      this.visible = true;
    },
    classifySelect(e) {
      this.typeList = [];
      if (e) {
        var typeList = [];
        var incomeList = this.incomeList;
        for (var i in incomeList) {
          if (incomeList[i].iccf0002 == e) {
            typeList.push(incomeList[i]);
          }
        }
        this.typeList = typeList;
      }
      this.$set(this.model, "ssba0010", e);
      this.$set(this.model, "ssba0011", "");
      this.$set(this.model, "ssba0012", "");
    },
    typeSelect(e) {
      this.wayList = [];
      if (e) {
        var wayList = [];
        var incomeList = this.incomeList;
        var iccf0008 = '';
        for (var i in incomeList) {
          if (incomeList[i].iccf0002 == e) {
            wayList.push(incomeList[i]);
          }
          if (incomeList[i].id == e) {
            iccf0008 = incomeList[i].iccf0008;
          }
        }
        this.wayList = wayList;
      }
      this.$set(this.model, "ssba0011", e);
      //this.$set(this.model, "ssba0012", "");
    },
    changeSsba0016(e) {
      if (this.model.ssba0012) {
        this.waySelect(this.model.ssba0012);
      }
      this.$set(this.model, "ssba0016", e);
    },
    waySelect(e) {
      if (e) {
        //自动计算月收入金额
        var income = this.needCheck(e);
        if (income) {
          let ssba0001 = this.model.ssba0001;
          this.ssba0006Disable = true;
          if (income.iccf0004 != 2 && income.id) {
            this.Loading = true;
            income.azdf0007 = this.$parent.queryParam.azdf0007;
            income.ssba0001 = ssba0001;
            income.type = this.type;
            income.azcp0500 = this.$parent.queryParam.azcp0500;
            income.ssba0016 = this.model.ssba0016;
            if (income.iccf0002 == '0106' && !this.model.ssba0016) {
              this.Loading = false;
              return false;
            }
            postAction(this.url.load0006, income).then((res) => {
              if (res.data.errcode != 0) {
                this.$Message.error(res.data.errmsg);
              } else {
                this.$set(this.model, "ssba0006", res.data.data);
              }
            }).finally(() => {
              this.Loading = false;
            });
          }
        } else {
          if (this.model.ssba0012) {
            this.model.ssba0006 = null;
          }
          this.ssba0006Disable = false;
        }
      }
      this.$set(this.model, "ssba0012", e);
    },
    //是否需要校验
    needCheck(e) {
      var incomeList = this.incomeList;
      if (e == '010211') {//实际收入金额
        return false;
      }
      for (var i in incomeList) {
        if (incomeList[i].id == e && incomeList[i].iccf0004 != 0) {
          return incomeList[i];
        }
      }
      return false;
    },
    change0014() {
      var val = this.model.ssba0014;
      if (!isNaN(parseFloat(val))) {
        this.$set(this.model, "ssba0006", val / 2);
      } else {
        this.model.ssba0014 = null;
      }
    },
    change0015() {
      var val = this.model.ssba0015;
      if (!isNaN(parseFloat(val))) {
        this.$set(this.model, "ssba0006", val / 2);
      } else {
        this.model.ssba0015 = null;
      }
    },
    async setIncomeList() {
      var that = this;
      var incomeList = [];
      if (sessionStorage.getItem("income_type")) {
        incomeList = JSON.parse(sessionStorage.getItem("income_type"));
      } else {
        var result = await postAction('/api/ac/subsistence/ss11003Service/typeList', {});
        if (result.data.data) {
          incomeList = result.data.data;
          sessionStorage.setItem("income_type", JSON.stringify(incomeList));
        }
      }
      that.incomeList = incomeList;
      var classifyList = [];
      for (var i in incomeList) {
        if (incomeList[i].iccf0003 == 1) {
          classifyList.push(incomeList[i]);
        }
      }
      this.classifyList = classifyList;
      this.wayList = [];
      this.typeList = [];
      if (this.model.ssba0012) {
        var ssba0012 = this.model.ssba0012;
        var iccf0009 = "";
        for (var i in incomeList) {
          if (incomeList[i].id == ssba0012) {
            iccf0009 = incomeList[i].iccf0009;
          }
        }
        var n = iccf0009.split(",");
        this.classifySelect(n[0]);
        this.typeSelect(n[1]);
        this.waySelect(ssba0012);
      }
    },
  }
}
</script>

<style scoped>
.itemStyle {
  width: 400px
}
</style>
